package myJs.projectCps.pcaTab

import cats.effect._
import com.softwaremill.quicklens._
import japgolly.scalajs.react.React.Fragment
import japgolly.scalajs.react._
import japgolly.scalajs.react.util.EffectCatsEffect._
import japgolly.scalajs.react.vdom.all._
import myJs.Implicits._
import myJs.Utils.layer
import myJs.api.Api
import myJs.myPkg.Hooks._
import myJs.myPkg.Swal.swal
import myJs.myPkg._
import myJs.myPkg.reactHookForm.{FormProvider, ReactHookForm, UseFormOptions, yupResolver}
import myJs.myPkg.tabs._
import myJs.projectCps._
import myJs.tool.Tool
import myJs.tool.Tool._
import myJs.user.FeatureSelectionTool
import shared.pojo.MissionTool.CheckFileExist
import shared.pojo.mission._
import shared.tool.DefaultPickle
import upickle.default._

import scala.scalajs.js
import scala.scalajs.js.{Date, JSON}

/** Created by yz on 3/11/2020
  */
object Cp {

  case class Props(idStr: String = "") {
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>
    val initTab = List("pca2D", "color", "font")(0)
    val (pcaBoxplotExist,setPcaBoxplotExist)=useState(true)
    val (tab, setTab) = useState(initTab)

    val idStr=props.idStr

    object FTool {

      def tabChange = (v: String) => {
        setTab(v)
      }

      def refreshPcaBoxplotExist={
        val fileName = "PCA_Score_with_Boxplot_with_Points.png"
        val finalFileName = FeatureSelectionTool.getFinalFileName(idStr, fileName)
        val request = CheckFileExist.Request(missionId = Tool.getMissionId, fileName = finalFileName)
        Api.missionTool.checkFileExist(request).flatMapSync { b =>
          setPcaBoxplotExist(b)
        }
      }

    }

    useEffect(
      {
        FTool.refreshPcaBoxplotExist
      },
      List(props.idStr)
    )

      Tabs(id = "tab", activeKey = tab, onSelect = FTool.tabChange, transition = false)(
        Tab(eventKey = "pca2D", title = s"${messages("pCAScore2D")}")(
          div(
            marginTop := "20px",
            pca2D.Cp
              .Props(
                idStr = props.idStr
              )
              .render
          )
        ),
        Tab(eventKey = "pca3D", title = s"${messages("pCAScore3D")}")(
          div(
            marginTop := "20px",
            pca3D.Cp
              .Props(
                idStr = props.idStr
              )
              .render
          )
        ),
        Tab(eventKey = "variance", title = s"${messages("pCAScreeplot")}")(
          div(
            marginTop := "20px",
            variance.Cp
              .Props(
                idStr = props.idStr
              )
              .render
          )
        ),
        if(pcaBoxplotExist){
          Tab(eventKey = "pcaBoxplot", title = s"${messages("pCAScoreWithBoxplotWithPoints")}")(
            div(
              marginTop := "20px",
              pcaBoxplot.Cp
                .Props(
                  idStr = props.idStr
                )
                .render
            )
          )
        }else EmptyVdom

      )

  }

}
